package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.aliyun.dyplsapi20170525.models.BindAxbResponseBody;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.dao.CommonPrivacyPhoneMapper;
import com.yungu.swift.common.model.dto.CommonPrivacyPhoneDto;
import com.yungu.swift.common.model.vo.PrivacyPhoneVo;
import com.yungu.swift.common.service.CommonPrivacyPhoneService;
import com.yungu.swift.common.util.PrivacyPhoneUtil;
import com.yungu.swift.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright © 厦门云谷互联信息科技有限公司 2018-2020 All Right Reserved
 * <p>
 * 版本信息业务逻辑接口实现类
 *
 * @author Zerrion
 * @version 1.1.0-SNAPSHOT
 * @since 2018-07-05 11:28
 */
@Service
@Slf4j
public class CommonPrivacyPhoneServiceImpl extends BaseServiceImpl<CommonPrivacyPhoneDto> implements CommonPrivacyPhoneService {

    @Autowired
    private CommonPrivacyPhoneMapper commonPrivacyPhoneMapper;

    @Override
    protected IMapper<CommonPrivacyPhoneDto> getMapper() {
        return commonPrivacyPhoneMapper;
    }

    /**
     * 获取虚拟号码
     *
     * @param privacyPhoneVo
     * @return
     */
    @Override
    public ResponseData<String> getPrivacyPhone(PrivacyPhoneVo privacyPhoneVo) throws Exception {
        List<CommonPrivacyPhoneDto> list = null;
        //查询数据库未过期的数据
        Map<String, Object> map = new HashMap<>();
        map.put("phoneNoA", privacyPhoneVo.getPhoneNoA());
        map.put("phoneNoB", privacyPhoneVo.getPhoneNoB());
        map.put("status", 0);
        list = commonPrivacyPhoneMapper.list(map);
        if (list.isEmpty()) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("phoneNoA", privacyPhoneVo.getPhoneNoB());
            map1.put("phoneNoB", privacyPhoneVo.getPhoneNoA());
            map1.put("status", 0);
            list = commonPrivacyPhoneMapper.list(map1);
        }
        privacyPhoneVo.setExpiration(PrivacyPhoneUtil.thirtyMinutesLater());
        if (list.isEmpty()) {
            int type = 0;
            while (true) {
                privacyPhoneVo.setType(type);
                BindAxbResponseBody bindAxbResponseBody = PrivacyPhoneUtil.BindAxb(privacyPhoneVo);
                if (bindAxbResponseBody.getCode().equals("OK")) {
                    CommonPrivacyPhoneDto commonPrivacyPhoneDto = new CommonPrivacyPhoneDto();
                    commonPrivacyPhoneDto.setUuid(StringUtils.buildUUID());
                    commonPrivacyPhoneDto.setPoolKey(PrivacyPhoneUtil.poolKey[type]);
                    commonPrivacyPhoneDto.setPhoneNoA(privacyPhoneVo.getPhoneNoA());
                    commonPrivacyPhoneDto.setPhoneNoB(privacyPhoneVo.getPhoneNoB());
                    commonPrivacyPhoneDto.setExpiration(privacyPhoneVo.getExpiration());
                    commonPrivacyPhoneDto.setRequestId(bindAxbResponseBody.getRequestId());
                    commonPrivacyPhoneDto.setSubsId(bindAxbResponseBody.getSecretBindDTO().getSubsId());
                    commonPrivacyPhoneDto.setSecretNo(bindAxbResponseBody.getSecretBindDTO().getSecretNo());
                    commonPrivacyPhoneDto.setStatus(0);
                    commonPrivacyPhoneDto.setCreateOn(new Date());
                    commonPrivacyPhoneMapper.add(commonPrivacyPhoneDto);
                    return ResponseData.buildSuccessResponse(bindAxbResponseBody.getSecretBindDTO().secretNo);
                } else if (bindAxbResponseBody.getCode().equals("isv.NO_AVAILABLE_NUMBER") ||
                        bindAxbResponseBody.getCode().equals("isv.EXCEED_LIMIT")) {
                    int size = PrivacyPhoneUtil.poolKey.length;
                    if (type < size) {
                        type++;
                    } else {
                        return ResponseData.buildErrorResponse(500, "无可用隐私号码");
                    }
                } else {
                    return ResponseData.buildErrorResponse(500, bindAxbResponseBody.getMessage());
                }
            }
        } else {
            //判断过期时间是否大于当前时间
            if (PrivacyPhoneUtil.TimeStampConversionExample(list.get(0).getExpiration())) {
                return ResponseData.buildSuccessResponse(list.get(0).getSecretNo());
            } else {
                CommonPrivacyPhoneDto updateDto = new CommonPrivacyPhoneDto();
                updateDto.setUuid(list.get(0).getUuid());
                updateDto.setStatus(1);
                commonPrivacyPhoneMapper.edit(updateDto);
                int type = 0;
                while (true) {
                    privacyPhoneVo.setType(type);
                    BindAxbResponseBody bindAxbResponseBody = PrivacyPhoneUtil.BindAxb(privacyPhoneVo);
                    if (bindAxbResponseBody.getCode().equals("OK")) {
                        CommonPrivacyPhoneDto commonPrivacyPhoneDto = new CommonPrivacyPhoneDto();
                        commonPrivacyPhoneDto.setUuid(StringUtils.buildUUID());
                        commonPrivacyPhoneDto.setPoolKey(PrivacyPhoneUtil.poolKey[type]);
                        commonPrivacyPhoneDto.setPhoneNoA(privacyPhoneVo.getPhoneNoA());
                        commonPrivacyPhoneDto.setPhoneNoB(privacyPhoneVo.getPhoneNoB());
                        commonPrivacyPhoneDto.setExpiration(privacyPhoneVo.getExpiration());
                        commonPrivacyPhoneDto.setRequestId(bindAxbResponseBody.getRequestId());
                        commonPrivacyPhoneDto.setSubsId(bindAxbResponseBody.getSecretBindDTO().getSubsId());
                        commonPrivacyPhoneDto.setSecretNo(bindAxbResponseBody.getSecretBindDTO().getSecretNo());
                        commonPrivacyPhoneDto.setStatus(0);
                        commonPrivacyPhoneDto.setCreateOn(new Date());
                        commonPrivacyPhoneMapper.add(commonPrivacyPhoneDto);
                        return ResponseData.buildSuccessResponse(bindAxbResponseBody.getSecretBindDTO().secretNo);
                    } else if (bindAxbResponseBody.getCode().equals("isv.NO_AVAILABLE_NUMBER") ||
                            bindAxbResponseBody.getCode().equals("isv.EXCEED_LIMIT")) {
                        int size = PrivacyPhoneUtil.poolKey.length;
                        if (type < size) {
                            type++;
                        } else {
                            return ResponseData.buildErrorResponse(500, "无可用隐私号码");
                        }
                    } else {
                        return ResponseData.buildErrorResponse(500, bindAxbResponseBody.getMessage());
                    }
                }
            }
        }
    }
}
